package com.springlearn.springsourcecode.chap12;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author wtcode
 * @Description
 * @date 2025/3/31 - 16:18
 */
public class CglibProxyDemo {

    static class Target{
        public void foo(){
            System.out.println("target foo");
        }
    }

    public static void main(String[] args) {

        Target target = new Target();

        Target proxy = (Target)Enhancer.create(Target.class, new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("before...");
                //Object invoke = method.invoke(target, args);
                //Object invoke = methodProxy.invoke(target, args);
                Object o1 = methodProxy.invokeSuper(o, args);
                //System.out.println("after...");
                return o1;
            }
        });

        proxy.foo();
    }
}
